package com.rongxun.hbwallet.ui.activities;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.rongxun.base.BaseActivity;
import com.rongxun.hbwallet.R;
import com.rongxun.hbwallet.constants.IntentKey;
import com.rongxun.scancode.qrcode.QRCodeEncode;
import com.rongxun.utils.RxUtil;
import com.rongxun.utils.StatusBarUtil;
import com.rongxun.utils.UUi;
import com.rongxun.utils.wallet.ETHWallet;

import java.io.ByteArrayOutputStream;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;
import io.reactivex.FlowableOnSubscribe;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * 创建者     庄丰泽
 * 创建时间   2018/8/11 14:24
 * 描述	      收款码
 */

public class ReceiptCodeActivity extends BaseActivity {
    @BindView(R.id.toolbar_title)
    TextView  mTitle;
    @BindView(R.id.toolbar_right_text)
    TextView  mRightText;
    @BindView(R.id.toolbar)
    Toolbar   mToolbar;
    @BindView(R.id.receiptcode_tv_address)
    TextView  mTvAddress;
    @BindView(R.id.receiptcode_btn_copyaddress)
    Button    mBtnCopyaddress;
    @BindView(R.id.receiptcode_iv_addressimg)
    ImageView mIvAddressimg;
    @BindView(R.id.toolbar_back)
    ImageView mToolbarBack;

    @Override
    protected void init() {
        StatusBarUtil.setToolBarPaddingTop(mToolbar, this);
        mTitle.setText(getString(R.string.receipt_code));
        mToolbarBack.setVisibility(View.VISIBLE);
        mRightText.setText(getString(R.string.share));
        mRightText.setVisibility(View.VISIBLE);
        ETHWallet wallet = getIntent().getParcelableExtra(IntentKey.ETH_WALLET);

        addSubscribe(Flowable.create((FlowableOnSubscribe<byte[]>) e -> {
            QRCodeEncode.Builder builder = new QRCodeEncode.Builder();
            builder.setBackgroundColor(0xffffff)
                    .setOutputBitmapHeight(UUi.dip2px(270))
                    .setOutputBitmapWidth(UUi.dip2px(270));
            Bitmap qrCodeBitmap = builder.build().encode(wallet.getAddress());


            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            qrCodeBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
            byte[] bytes = baos.toByteArray();
            e.onNext(bytes);
        }, BackpressureStrategy.BUFFER).compose(RxUtil.rxSchedulerHelper()).subscribe(new Consumer<byte[]>() {
            @Override
            public void accept(byte[] bytes) throws Exception {
                Glide.with(ReceiptCodeActivity.this)
                        .load(bytes)
                        .into(mIvAddressimg);
            }
        }));


        mTvAddress.setText(wallet.getAddress());


    }


    protected CompositeDisposable mCompositeDisposable;

    protected <T extends Disposable> T addSubscribe(T subscription) {
        if (mCompositeDisposable == null) {
            mCompositeDisposable = new CompositeDisposable();
        }
        mCompositeDisposable.add(subscription);


        return subscription;
    }

    protected void unSubscribe() {
        if (mCompositeDisposable != null) {
            mCompositeDisposable.dispose();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unSubscribe();
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_receiptcode;
    }

    @OnClick({R.id.toolbar_right_text, R.id.receiptcode_btn_copyaddress, R.id.toolbar_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.toolbar_right_text:
                share();
                break;
            case R.id.receiptcode_btn_copyaddress:
                copy();
                break;
            case R.id.toolbar_back:
                onBackPressed();
                break;
        }
    }

    private void copy() {
        ClipboardManager cm = (ClipboardManager) this.getSystemService(Context.CLIPBOARD_SERVICE);
        // 将文本内容放到系统剪贴板里。
        if (cm != null) {
            // 创建普通字符型ClipData
            ClipData mClipData = ClipData.newPlainText("Label", mTvAddress.getText().toString());
            // 将ClipData内容放到系统剪贴板里。
            cm.setPrimaryClip(mClipData);
        }
        mBtnCopyaddress.setText(R.string.derive_private_key_already_copy_btn);
        mBtnCopyaddress.setEnabled(false);
        mBtnCopyaddress.setTextColor(Color.WHITE);
    }

    private void share() {

    }
}
